package com.ly.base.file;

import lombok.AllArgsConstructor;

import java.io.*;
import java.util.UUID;

/**
 * @Classname FileOperator
 * @Description
 * 一.流的分类
 * 1.txt.数据单位：字节（中文乱码），字符(无法处理二进制文件，比如视频和图片）
 * 2.数据的流向：输入，输出
 * 3.对象：节点流（文件流），对象流，缓存流
 *
 * 二
 * 抽象基类      节点流             缓存流
 * InputStream  FileInputStream    BufferedInputStream
 * OutputStream FileOutputSteam    BufferedOutputStream
 * Reader       FileReader         BufferedReader
 * Writer       FileWriter         BufferedWriter
 *
 * 三
 * 转化流
 *
 * @Date 2020/10/30 21:18
 * @Author 冷心影翼
 */
public class FileOperator {

	public static void main(String[] args) throws Exception {
		fileReaderWriter();
		fileInputStreamAndOutputSteam();
		bufferReaderWriter();
		bufferInputStreamAndOuputStream();
		InputStreamReaderAndOutputStreamWriter();
		ObjectInputOutput();
	}

	public static void fileInputStreamAndOutputSteam() throws IOException {
		FileInputStream fis = new FileInputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\1.txt");
		FileOutputStream fos = new FileOutputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\fileStream.txt");
		byte[] data = new byte [1024];
		int len;
		while ((len=fis.read(data))!=-1) {

			fos.write(data,0,len);
		}
		fis.close();
		fos.close();
	}


	public static void fileReaderWriter() throws Exception {
		File file = new File("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\1.txt");
		FileReader fr = new FileReader(file);
		FileWriter fw = new FileWriter("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\fileWrite.txt");
		char[] data = new char[5];
		int i;
		while ((i = fr.read(data))!=-1){
			fw.write(data,0,i);
		}
		fr.close();
		fw.close();
	}

	public static void bufferReaderWriter() throws IOException {
		File file = new File("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\1.txt");
		FileReader fr = new FileReader(file);
		FileWriter fw = new FileWriter("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\bufferWrite.txt");
		BufferedReader br = new BufferedReader(fr);
		BufferedWriter bw = new BufferedWriter(fw);
		String line;
		while ((line = br.readLine())!=null) {
			bw.write(line);
			bw.newLine();
//			bw.flush();
		}
		br.close();
		bw.close();
	}

	public static void bufferInputStreamAndOuputStream() throws IOException {
		FileInputStream fis = new FileInputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\1.txt");
		FileOutputStream fos = new FileOutputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\bufferStream.txt");
		BufferedInputStream bis = new BufferedInputStream(fis);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		byte[] data = new byte[1024];
		int len;
		while ((len = bis.read(data))!=-1) {
			bos.write(data,0,len);
//			bos.flush();
		}

		bis.close();
		bos.close();
	}

	public static void InputStreamReaderAndOutputStreamWriter() throws IOException {
		FileInputStream fis = new FileInputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\1.txt");
		FileOutputStream fos = new FileOutputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\OutputStreamWriter.txt");
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
		OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");

		char[] data = new char[1024];
		int len;
		while ((len = isr.read(data))!=-1) {
			osw.write(data,0,len);
		}

		isr.close();
		osw.close();
	}

	public static void ObjectInputOutput() throws IOException, ClassNotFoundException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\object.dat"));
		oos.writeObject(new FileObject(1,"a"));
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\IdeaCode\\frame\\interview\\src\\main\\java\\com\\ly\\base\\file\\object.dat"));

		FileObject o = (FileObject) ois.readObject();
		System.out.println(o.str);
	}

	@AllArgsConstructor
	static class FileObject implements Serializable{
		private static final long serialVersionUID = 12312345634L;

		int a = 0;
		String str = "1";
	}
}
